﻿using System;
using System.Drawing;
using System.IO;
using System.Linq;
using DXEasyAccount.Common;
using DevExpress.Data;
using DevExpress.Utils;
using DevExpress.Utils.Menu;
using DevExpress.XtraBars.Docking;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;

namespace ContractManager.Common.Service
{
    /// <summary>
    /// Column view service. 
    /// </summary>
    /// <remarks>
    /// Golovkov Alex, 29.09.2010. 
    /// </remarks>
    internal static class ColumnViewService
    {
        #region Public Methods

        

        /// <summary>
        /// Restore layout. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 04.10.2010. 
        /// </remarks>
        /// <param name="fileName">
        /// Filename of the file. 
        /// </param>
        /// <param name="dockManager">
        /// Manager for dock. 
        /// </param>
        public static void RestoreLayout(string fileName, DockManager dockManager)
        {
            if (!File.Exists(fileName))
            {
                return;
            }

            using (var f = new FileStream(fileName, FileMode.Open))
            {
                dockManager.RestoreLayoutFromStream(f);
            }
        }

        /// <summary>
        /// Restore layout. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 04.10.2010. 
        /// </remarks>
        /// <param name="fileName">
        /// Filename of the file. 
        /// </param>
        /// <param name="columnView">
        /// The column view. 
        /// </param>
        public static void RestoreLayout(string fileName, ColumnView columnView)
        {
            if (!File.Exists(fileName))
            {
                return;
            }

            using (var f = new FileStream(fileName, FileMode.Open))
            {
                columnView.RestoreLayoutFromStream(f);
            }
        }

        /// <summary>
        /// Saves a layout. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 04.10.2010. 
        /// </remarks>
        /// <param name="fileName">
        /// Filename of the file. 
        /// </param>
        /// <param name="dockManager">
        /// Manager for dock. 
        /// </param>
        public static void SaveLayout(string fileName, DockManager dockManager)
        {
            using (var f = new FileStream(fileName, FileMode.Create))
            {
                dockManager.SaveLayoutToStream(f);
            }
        }

        /// <summary>
        /// Saves a layout. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 04.10.2010. 
        /// </remarks>
        /// <param name="fileName">
        /// Filename of the file. 
        /// </param>
        /// <param name="columnView">
        /// The column view. 
        /// </param>
        public static void SaveLayout(string fileName, ColumnView columnView)
        {
            using (var f = new FileStream(fileName, FileMode.Create))
            {
                columnView.SaveLayoutToStream(f);
            }
        }

        /// <summary>
        /// Sets a cell editor. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 15.12.2010. 
        /// </remarks>
        /// <param name="e">
        /// Custom row cell edit event information. 
        /// </param>
        /// <param name="editor">
        /// The editor. 
        /// </param>
        public static void SetCellEditor(CustomRowCellEditEventArgs e, RepositoryItem editor)
        {
            e.RepositoryItem = editor;
        }

        /// <summary>
        /// The set data.
        /// </summary>
        /// <param name="s">
        /// The DXMenu item
        /// </param>
        /// <param name="valueText">
        /// The value text.
        /// </param>
        /// <typeparam name="T">
        /// instance object type
        /// </typeparam>
        public static void SetData<T>(DXMenuItem s, T valueText)
        {
            if (s.Tag == null)
            {
                return;
            }

            var column = (GridColumn)s.Tag;
            column.SortOrder = ColumnSortOrder.None;
            var view = column.View;
            foreach (var i in view.GetSelectedRows())
            {
                view.SetRowCellValue(i, column, valueText);
            }
        }

        /// <summary>
        /// The set data. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 10.11.2010. 
        /// </remarks>
        /// <typeparam name="T">
        /// instance object type 
        /// </typeparam>
        /// <param name="s">
        /// grid column        The DXMenu item. 
        /// </param>
        /// <param name="valueText">
        /// The value text. 
        /// </param>
        public static void SetData<T>(GridColumn s, T valueText)
        {
            if (s == null)
            {
                return;
            }

            var column = s;
            column.SortOrder = ColumnSortOrder.None;
            var view = column.View;
            foreach (var i in view.GetSelectedRows())
            {
                view.SetRowCellValue(i, column, valueText);
            }
        }

        /// <summary>
        /// Setting column grid view. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 29.09.2010. 
        /// </remarks>
        /// <param name="gridView">
        /// The grid view. 
        /// </param>
        public static void SettingColumnGridView(GridView gridView)
        {
            foreach (var column in
                gridView.Columns.Cast<GridColumn>().Where(
                    column => column.FieldName != "TikerShow" && column.FieldName != "D2"))
            {
                column.Visible = false;
            }

            gridView.Columns["TikerShow"].VisibleIndex = 0;
            gridView.Columns["D2"].DisplayFormat.FormatType = FormatType.DateTime;
            gridView.Columns["D2"].DisplayFormat.FormatString = "G";
        }

        /// <summary>
        /// Text decimal validating. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 17.12.2010. 
        /// </remarks>
        /// <param name="sender">
        /// Source of the event. 
        /// </param>
        public static void TxtDecimalValidating(object sender)
        {
            var textEdit = sender as TextEdit;

            if (textEdit == null)
            {
                return;
            }

            try
            {
                var s = textEdit.EditValue.ToString();
                var value = TextToNumberService.ReplaceTexValue(s);
                textEdit.EditValue = value;
            }
            catch (Exception)
            {
                XtraMessageBox.Show("Введите корректные данные");
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The set red fore color.
        /// </summary>
        /// <param name="view">
        /// The column view.
        /// </param>
        /// <param name="e">
        /// The  RowCellStyleEventArgs.
        /// </param>
        /// <param name="gridColumn">
        /// The grid column.
        /// </param>
        internal static void SetRedForeColor(ColumnView view, RowCellStyleEventArgs e, GridColumn gridColumn)
        {
            if (e.Column != gridColumn)
            {
                return;
            }

            var a1 = view.GetRowCellDisplayText(e.RowHandle, gridColumn);
            decimal d1;

            if (Decimal.TryParse(a1, out d1))
            {
                if (d1 >= 0)
                {
                    return;
                }
            }

            e.Appearance.ForeColor = Color.Red;
            e.Appearance.Options.UseForeColor = true;
        }

        /// <summary>
        /// The set red fore color.
        /// </summary>
        /// <param name="view">
        /// The column view.
        /// </param>
        /// <param name="e">
        /// The RowCellCustomDrawEventArgs.
        /// </param>
        /// <param name="gridColumn">
        /// The grid column.
        /// </param>
        internal static void SetRedForeColor(ColumnView view, RowCellCustomDrawEventArgs e, GridColumn gridColumn)
        {
            if (e.Column != gridColumn)
            {
                return;
            }

            var a1 = view.GetRowCellDisplayText(e.RowHandle, gridColumn);
            decimal d1;

            if (Decimal.TryParse(a1, out d1))
            {
                if (d1 >= 0)
                {
                    return;
                }
            }

            e.Appearance.ForeColor = Color.Red;
            e.Appearance.Options.UseForeColor = true;
        }

        

        /// <summary>
        /// The set caption.
        /// </summary>
        /// <param name="column">
        /// The column.
        /// </param>
        /// <param name="caption">
        /// The caption.
        /// </param>
        private static void SetCaption(GridColumn column, string caption)
        {
            if (column == null)
            {
                return;
            }

            column.Caption = caption;
        }

        

        #endregion
    }
}
